package com.server.connection;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
import com.core.log.Debug;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Vector;

/**
 *
 * @author daz
 */
public class WriterThread implements Runnable {

    private static final int THREAD_SLEEP = 10;
    private DataOutputStream outputStream;
    private Vector outgoingMessage;
    private boolean isRunning;
    private Socket socket;

    public WriterThread(DataOutputStream out, Socket socket) {
        this.outputStream = out;
        this.socket = socket;
        this.outgoingMessage = new Vector();
        isRunning = true;
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        while (isRunning) {
            try {
                Thread.sleep(THREAD_SLEEP);
            } catch (InterruptedException ex) {
            }
            flushOutputQueue();
        }
        try {
            if (outputStream != null) {
                outputStream.close();
                outputStream = null;
            }
        } catch (IOException ex) {
            isRunning = false;
        }
    }

    public boolean queueMessage(String s) {
        try {
            System.out.println("Send to client: " + s);
//            Debug.d("Send to client:" + s);
            int dataLength = s.getBytes("UTF-8").length + 2;
            byte[] outputData = new byte[dataLength];
            for (int i = 1; i < dataLength - 1; i++) {
                outputData[i] = s.getBytes("UTF-8")[i - 1];
            }
            outputData[0] = 125;
            outputData[dataLength - 1] = 38;
            return queueMessage(outputData);
        } catch (Exception ex) {
            return false;
        }

    }

    public void sendImediate(String message) {
        try {
            int dataLength = message.getBytes("UTF-8").length + 2;
            byte[] outputData = new byte[dataLength];
            for (int i = 1; i < dataLength - 1; i++) {
                outputData[i] = message.getBytes("UTF-8")[i - 1];
            }
            outputData[0] = 125;
            outputData[dataLength - 1] = 38;
            outputStream.write(outputData);
            outputStream.flush();
        } catch (Exception ex) {
        }
    }

    public boolean queueMessage(byte[] byteArr) {
//        Debug.d("add to out going");
        if (outgoingMessage != null) {
            outgoingMessage.add(byteArr);
            return true;
        }
        return false;
    }

    public void stop() {
        isRunning = false;
        outgoingMessage = null;
    }

    public void flushOutputQueue() {
        if (outgoingMessage != null) {
            if (outgoingMessage.size() > 0) {
                byte[] message = null;
                message = (byte[]) outgoingMessage.elementAt(0);
                if (message != null) {
                    try {
                        outputStream.write(message);
                        outputStream.flush();
//                        Debug.d("send success ");
                        outgoingMessage.removeElementAt(0);
                    } catch (IOException ex) {
                        stop();
                    }
                }
            }
        }
    }
}
